<template>
  <div>
    <div style="margin-top: 10px">
      <el-form ref="form" :model="form" label-width="80px" class="">
        <el-form-item class="form-item" label="模型名称">
          <el-input v-model="form.name" placeholder="模型名称"></el-input>
        </el-form-item>
        <el-form-item class="form-item">
          <el-button class="form-btn" type="primary" @click="onQuery">查询</el-button>
          <el-button class="form-btn" @click="clearQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <!--增改删导出-->
    <el-row>
      <el-button class="tag-btn" type="success" plain @click="addMVisible = true">新增</el-button>
      <el-button class="tag-btn" type="warning" plain @click="modifyItf('modify')">修改</el-button>
      <el-button class="tag-btn" type="danger" plain @click="delItf('del')">删除</el-button>
      <el-button class="tag-btn" type="info" plain @click="outputItfData">导出</el-button>
      <template>
        <el-input-number class="curPageCounter" @change="handleCurrentChange" v-model="curPage" :min="1" :max="pages" label="描述文字"></el-input-number>
      </template>
    </el-row>

    <!--列表-->
    <template>
      <el-table
        class="interface-table"
        ref="multipleTable"
        :data="MtableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          align="center"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          align="center"
          prop="id"
          label="流程编号"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="流程名称"
          width="200">
        </el-table-column>
        <el-table-column
          align="center"
          prop="key"
          label="流程标识"
          width="200">
        </el-table-column>
        <el-table-column
          align="center"
          prop="version"
          label="流程版本"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          prop="createTime"
          label="创建时间"
          width="150">
        </el-table-column>
        <el-table-column
          align="center"
          prop="updateTime"
          label="更新时间"
          width="200">
        </el-table-column>
        <el-table-column
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination
          background
          class="interpage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="curPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="mtotal">
        </el-pagination>
      </div>
    </template>
    <!--新增接口对话框-->
    <el-dialog
      title="新增流程模型"
      :visible.sync="addMVisible"
      width="40%">
      <el-form label-width="80px">
        <el-form-item label="流程名称">
          <el-input v-model="addItfForm.name" placeholder="请输入流程名称"></el-input>
        </el-form-item>
        <el-form-item label="流程标识">
          <el-input v-model="addItfForm.key" placeholder="请输入流程标识"></el-input>
        </el-form-item>
        <el-form-item label="流程描述">
          <el-input
            style="font-size: 16px;"
            type="textarea"
            :rows="5"
            placeholder='请输入流程描述'
            v-model="addItfForm.description">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addMVisible = false">取 消</el-button>
        <el-button type="primary" @click="addItf">提交</el-button>
      </span>
    </el-dialog>

    <!--修改接口对话框-->
    <el-dialog
      title="修改角色"
      :visible.sync="modifyRoleVisible"
      width="40%">
      <el-form label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="modifyRoleForm.name" :placeholder="modifyRoleForm.name" disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="接口URL">
          <el-input v-model="modifyRoleForm.desc" :placeholder="modifyRoleForm.desc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyRoleVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModifyItf">提交</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: "Define",
    props: {
    },
    data() {
      return {
        /*查询表单*/
        form: {
          name: ''
        },
        /*接口列表*/
        MtableData: [],
        multipleSelection: [],
        /*分页*/
        curPage: 0,
        size: 5,
        mtotal: 0,
        pages: 0,
        addMVisible: false,
        addItfForm: {
          name: '',
          key: "",
          description: '',
        },
        modifyRoleVisible: false,
        modifyRoleForm: {
          name: '',
          desc: '',
        },
        //系统资源列表
        allResData: [],
        allJKResData: [],
        allCDResData: [],
        //当前角色所拥有的资源列表
        selfResData: [],
        selfJKResData: [],
        selfCDResData: [],
        renderFunc(h, option) {
          return <span>{ option.key } - { option.label }</span>;
        },
        //授权按钮初始可见
        authBtnVisial: true,
        //授权DIV可见
        authDivVisial: false,
        authDivResType: 'JK',

        authRoleId: 0
      }
    },
    mounted: function(){
      this.queryMItfList();
    },
    computed: {
      headers() {
        return {
          Authentication: this.$store.state.token,
          RefreshToken: this.$store.state.refresh_token
        };
      }
    },
    methods: {
      handleSwithEnable(scope){
        this.$http.post("/role/info/isEnableRole", {
          "roleId": scope.row.roleId,
          "isEnabled": scope.row.enabled
        }).then(resp => {
          if(resp.data.errCode === 200){
            if(scope.row.enabled){
              this.$message.success("角色["+scope.row.roleName+"]已启用");
            }else{
              this.$message.success("角色["+scope.row.roleName+"]已停用");
            }
          }
        })
      },
      resetInterfaceQueryForm(form) {
        this.$refs[form].resetFields();
      },
      //接口查询
      onQuery(){
        this.queryMItfList();
        console.log("查询成功！");
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //授权处理
      handleAuthrize(index, row){
        console.log(index, row);
        this.authDivVisial = true;
        this.authRoleId = row.roleId;
        this.authrizeResource(row.roleId);
      },
      handleEdit(index, row) {
        console.log(index, row);
        this.modifyItf(row.roleId);
      },
      handleDelete(index, row) {
        console.log(index, row);
        if(confirm("确认要删除？")){
          this.delItf(row.id);
        };
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.size = val;
        this.queryMItfList();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.curPage = val;
        this.queryMItfList();
      },
      //提交新增接口
      addItf(){
        this.$http.post("/process/model/create", {
          "name": this.addItfForm.name,
          "key": this.addItfForm.key,
          "description": this.addItfForm.description,
        }).then(resp => {
          if(resp.data.errCode === 200){
            this.$message.success("新增成功！");
            this.addMVisible = false;
            this.queryMItfList();
            this.clearAddItfForm();
            this.$router.push({path:"/process/editor/Editor?modelId=" + resp.data.data});
          }
        });
        console.log("提交成功！");
      },
      //修改按钮(oprNum操作数为1时则为“修改”功能，否则为编辑)
      modifyItf(oprNum){
        //修改
        if(oprNum === 'modify'){
          //判断是否已选择
          if(this.multipleSelection.length !== 1){
            this.$message.error("请选择一条数据！");
            return;
          }
          this.modifyRoleVisible = true;
          this.multipleSelection.forEach(t => {
            this.queryOne(t.roleId);
          });
        }else { //编辑
          this.queryOne(oprNum);
        }
      },
      //删除
      delItf(oprNum){
        //删除按钮
        if(oprNum === 'del'){
          //判断是否已选择
          if(this.multipleSelection.length !== 1){
            this.$message.error("请至少选择一条数据！");
            return;
          }
          if(confirm("确认要删除？")){
            this.multipleSelection.forEach(t => {
              this.submitDelItf(t.id);
            });
          }
        }else { //操作中的删除
          this.submitDelItf(oprNum);
        }
      },

      //修改接口
      submitModifyItf(){
        this.$http.post("/role/info/mod", {
          "roleName": this.modifyRoleForm.name,
          "roleDesc": this.modifyRoleForm.desc,
        }).then(resp => {
          if(resp.data.errCode === 200){
            this.$message.success("接口修改成功！");
            this.modifyRoleVisible = false;
            this.queryMItfList();
          }
        })
      },

      //导出接口数据
      outputItfData(){
        //判断是否已选择
        if(this.multipleSelection.length === 0){
          this.$message.error("请至少选择一条数据！");
          return;
        }
        this.$http({
          method: "post",
          url: "/mock/interface/export",
          data: this.multipleSelection,
          responseType: "blob"
        }).then(resp => {
          let url = window.URL.createObjectURL(new Blob([resp.data])); //创建blob,设置文件类型
          let a = document.createElement('a'); //创建a标签
          a.download = "接口数据表.xls"; //指定文件名
          a.style.display = 'none';
          a.href = url; //创建url
          a.click(); //click事件触发下载
          // URL.revokeObjectURL(a); //释放内存
        });
      },

      //查询接口列表
      queryMItfList(){
        this.$http.post("/process/model/data", {
          "curPage": this.curPage,
          "size": this.size,
          "modelName": this.form.name
        }).then(resp => {
          if (resp.data.errCode === 200){
            this.MtableData = resp.data.data.data;
            this.mtotal = resp.data.data.totals;
            console.log("query success!");
          }
        })
      },

      //查询单个接口
      queryOne(id){
        this.$http.post("/role/info/query/"+id).then(resp => {
          if(resp.data.errCode === 200){
            this.modifyRoleVisible = true;
            this.modifyRoleForm.name = resp.data.data.roleName;
            this.modifyRoleForm.desc = resp.data.data.roleDesc;
          }
        })
      },

      //删除单个接口
      submitDelItf(id){
        this.$http.post("/process/model/delete/"+id).then(resp => {
          if(resp.data.errCode === 200){
            this.queryMItfList();
            this.$message.success("删除成功！");
          }
        })
      },

      //清空查询条件
      clearQuery(){
        this.form = {
          name: ''
        }
      },
      clearAddItfForm(){
        this.addItfForm = {
          name: '',
          key: '',
          description: ''
        }
      },

    }
  }
</script>

<style scoped>
  .form-item{
    margin-left: 20px;
    width: 20%;
    display: inline-block;
  }
  .form-btn{
    position: relative;
    left: -60px;
  }
  .tag-btn{
    margin-left: 20px;
  }
  .interface-table{
    position: relative;
    left: 20px;
    top:10px;
  }
  .interpage{
    margin-top: 20px;
    margin-left: 20px;
    margin-bottom: 20px;
  }
  .curPageCounter{
    float: right;
    right: 40px;
  }
</style>
